<template>
  <el-button @click="handleClick" type="primary">
    <slot></slot>
  </el-button>
  <div class="icon-selector-dialog-body">
    <el-dialog :title="title" v-model="visible">
      <div class="container">
        <div class="item" v-for="(item, index) in Object.keys(Icons)" :key="index" @click="clickItem(item)">
          <div>
            <component :is="`el-icon-${toLine(item)}`"></component>
          </div>
          <div>{{item}}</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {watch} from "vue";
import {toLine} from "../../../utils";
import * as Icons from '@element-plus/icons-vue'
import {useCopy} from "../../../hooks/useCopy";

let props = defineProps<{
  title: string,
  visible: boolean
}>()

let emits = defineEmits(['update:visible'])

let handleClick = () => {
  emits('update:visible', !props.visible)
}

watch(() => props.visible, value => {
  emits('update:visible', value)
})

let clickItem = (item: string) => {
  let text = `el-icon-${toLine(item)}`
  useCopy(text)
  emits('update:visible', false)
}
</script>

<style lang="scss" scoped>
.container {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.item {
  width: 25%;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  margin-bottom: 20px;
  cursor: pointer;
}

svg {
  width: 2em;
  height: 2em;
}
</style>